package WordHandling;



import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import project3.StemInfo.StemInstance;

public class GUI implements ActionListener{
     //Tokenizer t= new Tokenizer();
	 static JPanel pane = new JPanel();
	 static JFrame frame = new JFrame("Queries");
	 static JButton search = new JButton("Search");
	 static JLabel title = new JLabel("Queries");
	 static JTextField query= new JTextField(30);
	 //Query application will appear on this Frame...like google's home page but ugly
	 static File dir = new File("");
		static ArrayList<File> fs = new ArrayList<File>();
		static ArrayList<StemInstance> db = new ArrayList<StemInstance>();
		InvertedIndexer index;
		//Pass arraylist return from tokenizer to the indexer.
   	 	
    public GUI(){}
    
    public void run(){
     pane.add(title);
     pane.add(query);
     pane.add(search);
     
     search.addActionListener(new GUI()); //add action listener for query box
     
     frame.add(pane);
     frame.setSize(750,300);
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     frame.setVisible(true);
    

     
    //File Chooser gets directory containing all files to be processed
     
      String curDir = System.getProperty("user.dir");
      JFileChooser fc = new JFileChooser(curDir);
      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      int returnVal = fc.showOpenDialog(null);
      if(returnVal == JFileChooser.APPROVE_OPTION) {
       File selection = fc.getSelectedFile();
    	   System.out.println("You chose to open this file: " + selection.getName());
    	   dir = selection;
    	 //Tokenize takes the entire directory, breaks it down into lists of strings excluding stop words and punctuation
      }
    }
    	 //Pass arraylist return from tokenizer to the indexer.
     
    	 //db = index.allInstances();


    //pass arraylist return from tokenizer to the indexer
    //when all preprocessing of the text is done listen for queries and display the graphical results
    

	@Override
	public void actionPerformed(ActionEvent e) {
		boolean empty_case=false;
		//Stores parsed and stemmed query
		ArrayList<String> parsed_query = new ArrayList<String>();
		
		//gets the query keywords from the GUI
		String query_input = query.getText();
		
		//Tokenizes the query and assigns returned list of strings to parsed_query
		Tokenizer query_tokenizer = new Tokenizer(1); 
		//can probably make some of these members static...
		query_tokenizer.tokenize(query_input);
		parsed_query = query_tokenizer.getQueryStrings();
		
		//stems the query
		Stemmer stemmer = new Stemmer();
		for(int i=0; i<parsed_query.size();i++){
			parsed_query.set(i, stemmer.stem(parsed_query.get(i)));
		}
		
		index = new InvertedIndexer(dir);
		
		//If nothing is searched returns visual for entire collection
		if(query_input.equals("")){
			empty_case=true;
			System.out.println("entering this loop");
			parsed_query = index.allInstances();
			System.out.println("query size "+ parsed_query.size());
		}
		
		for(int i=0; i<parsed_query.size(); i++){
			ArrayList<StemInstance> stems = new ArrayList<StemInstance>();
			stems = (ArrayList<StemInstance>) index.getInstances(parsed_query.get(i));
			for(int j=0; j<stems.size(); j++){
				db.add(stems.get(j));
			}
			//for(StemInstance s:(ArrayList<StemInstance>) index.getInstances(parsed_query.get(i))){
			//	temp.add(s);
			//}
		}
			
			//constructor of visualization searches database and creates interactive visualization
		   JFrame frame = new visualization(parsed_query,db,empty_case);
		   db.clear();
		    //frame.addWindowListener(new WindowAdapter(){
		    //  public void windowClosing(WindowEvent we){
		    //    System.exit(0);
		     // }
		   // });
		    //make this even a little bigger???
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		    frame.setSize(1000, 1000); //make this really really big
		    frame.setVisible(true);
		
		  }
	 	 
    
}
